#ifdef _DEBUG
#pragma comment(lib, "../Debug/PowerLibraryEngine.lib")
#else
#pragma comment(lib, "../Release/PowerLibraryEngine.lib")
#endif

#include "../PowerLibraryEngine/include/PowerLibraryEngine.h"
#include "LoadingData.h"

IDrawableSceneNode *Cube = NULL;
ILightSceneNode *li = NULL;
I3DCameraSceneNode *Camera3D = NULL;
I2DCameraSceneNode *Camera2D = NULL;

#define POS_CHANGE		(vPos)0.001
#define SCALE_CHANGE	(vPos)0.01
#define ROTATION_CHANGE	(vPos)0.01
#define LIGHT_POS_CHANGE	(vPos)0.01
#define LIGHT_COL_CHANGE	(vPos)0.01

void Update()
{
	if (InputManager->GetKeyState(KEY_KEY_Z)->PressedDown)
	{
		vector3dvpos Pos = Cube->GetPosition();

		if (InputManager->GetKeyState(KEY_KEY_1)->PressedDown)
		{
			Pos += POS_CHANGE;
			Cube->SetPosition(Pos);
		}
		if (InputManager->GetKeyState(KEY_KEY_2)->PressedDown)
		{
			Pos -= POS_CHANGE;
			Cube->SetPosition(Pos);
		}

		vector3dvpos Scale = Cube->GetScale();
		if (InputManager->GetKeyState(KEY_KEY_3)->PressedDown)
		{
			Scale.X += SCALE_CHANGE;
			Cube->SetScale(Scale);
		}
		if (InputManager->GetKeyState(KEY_KEY_4)->PressedDown)
		{
			Scale.X -= SCALE_CHANGE;
			Cube->SetScale(Scale);
		}

		vector3dvpos Rotation = Cube->GetRotation();
		if (InputManager->GetKeyState(KEY_KEY_5)->PressedDown)
		{
			Rotation.X += ROTATION_CHANGE;
			Cube->SetRotation(Rotation);
		}
		if (InputManager->GetKeyState(KEY_KEY_6)->PressedDown)
		{
			Rotation.X -= ROTATION_CHANGE;
			Cube->SetRotation(Rotation);
		}

		if (InputManager->GetKeyState(KEY_KEY_7)->PressedDown)
		{
			Pos.X += POS_CHANGE;
			Pos.Y += POS_CHANGE;
			Cube->SetPosition(Pos);
		}
		if (InputManager->GetKeyState(KEY_KEY_8)->PressedDown)
		{
			Pos.X -= POS_CHANGE;
			Pos.Y -= POS_CHANGE;
			Cube->SetPosition(Pos);
		}
		if (InputManager->GetKeyState(KEY_KEY_9)->PressedDown)
		{
			Scale.X += SCALE_CHANGE;
			Scale.Y += SCALE_CHANGE;
			Cube->SetScale(Scale);
		}
		if (InputManager->GetKeyState(KEY_KEY_0)->PressedDown)
		{
			Scale.X -= SCALE_CHANGE;
			Scale.Y -= SCALE_CHANGE;
			Cube->SetScale(Scale);
		}
	}
/*
	if (InputManager->GetKeyState(KEY_KEY_M)->PressedDown)
	{
		if (InputManager->GetKeyState(KEY_KEY_P)->PressedDown)
		{
			li->SetPositionX(li->GetPositionX()+LIGHT_POS_CHANGE);
			pLogDebug("Pos light: (%f,%f,%f)\n",li->GetPositionX(),li->GetPositionY(),li->GetPositionZ());
		}
		if (InputManager->GetKeyState(KEY_KEY_I)->PressedDown)
		{
			li->SetPositionX(li->GetPositionX()-LIGHT_POS_CHANGE);
			pLogDebug("Pos light: (%f,%f,%f)\n",li->GetPositionX(),li->GetPositionY(),li->GetPositionZ());
		}
		if (InputManager->GetKeyState(KEY_KEY_O)->PressedDown)
		{
			li->SetPositionY(li->GetPositionY()+LIGHT_POS_CHANGE);
			pLogDebug("Pos light: (%f,%f,%f)\n",li->GetPositionX(),li->GetPositionY(),li->GetPositionZ());
		}
		if (InputManager->GetKeyState(KEY_KEY_L)->PressedDown)
		{
			li->SetPositionY(li->GetPositionY()-LIGHT_POS_CHANGE);
			pLogDebug("Pos light: (%f,%f,%f)\n",li->GetPositionX(),li->GetPositionY(),li->GetPositionZ());
		}
		if (InputManager->GetKeyState(KEY_KEY_N)->PressedDown)
		{
			li->SetPositionZ(li->GetPositionZ()+LIGHT_POS_CHANGE);
			pLogDebug("Pos light: (%f,%f,%f)\n",li->GetPositionX(),li->GetPositionY(),li->GetPositionZ());
		}
		if (InputManager->GetKeyState(KEY_KEY_B)->PressedDown)
		{
			li->SetPositionZ(li->GetPositionZ()-LIGHT_POS_CHANGE);
			pLogDebug("Pos light: (%f,%f,%f)\n",li->GetPositionX(),li->GetPositionY(),li->GetPositionZ());
		}
	}
	if (InputManager->GetKeyState(KEY_ADD)->PressedDown)
	{
		if (InputManager->GetKeyState(KEY_KEY_P)->PressedDown)
		{
			CColor a(li->GetLightColor()->GetAmbient());
			a.r -= LIGHT_COL_CHANGE;
			a.b -= LIGHT_COL_CHANGE;
			pLogDebug("Col Amb: (%f,%f,%f)\n",li->GetLightColor()->GetAmbient().r,li->GetLightColor()->GetAmbient().g,li->GetLightColor()->GetAmbient().b);
		}
		if (InputManager->GetKeyState(KEY_KEY_I)->PressedDown)
		{
			CColor a(li->GetLightColor()->GetAmbient());
			a.r += LIGHT_COL_CHANGE;
			a.b += LIGHT_COL_CHANGE;
			pLogDebug("Col Amb: (%f,%f,%f)\n",li->GetLightColor()->GetAmbient().r,li->GetLightColor()->GetAmbient().g,li->GetLightColor()->GetAmbient().b);
			li->GetLightColor()->SetAmbient(a);
		}
		if (InputManager->GetKeyState(KEY_KEY_O)->PressedDown)
		{
			CColor a(li->GetLightColor()->GetDifuse());
			a.r -= LIGHT_COL_CHANGE;
			a.b -= LIGHT_COL_CHANGE;
			pLogDebug("Col Dif: (%f,%f,%f)\n",li->GetLightColor()->GetDifuse().r,li->GetLightColor()->GetDifuse().g,li->GetLightColor()->GetDifuse().b);
			li->GetLightColor()->SetDifuse(a);
		}
		if (InputManager->GetKeyState(KEY_KEY_L)->PressedDown)
		{
			CColor a(li->GetLightColor()->GetDifuse());
			a.r += LIGHT_COL_CHANGE;
			a.b += LIGHT_COL_CHANGE;
			pLogDebug("Col Dif: (%f,%f,%f)\n",li->GetLightColor()->GetDifuse().r,li->GetLightColor()->GetDifuse().g,li->GetLightColor()->GetDifuse().b);
			li->GetLightColor()->SetDifuse(a);
		}
		if (InputManager->GetKeyState(KEY_KEY_N)->PressedDown)
		{
			li->SetPositionZ(li->GetPositionZ()+LIGHT_POS_CHANGE);
			pLogDebug("Pos light: (%f,%f,%f)\n",li->GetPositionX(),li->GetPositionY(),li->GetPositionZ());
		}
		if (InputManager->GetKeyState(KEY_KEY_B)->PressedDown)
		{
			li->SetPositionZ(li->GetPositionZ()-LIGHT_POS_CHANGE);
			pLogDebug("Pos light: (%f,%f,%f)\n",li->GetPositionX(),li->GetPositionY(),li->GetPositionZ());
		}
	}
*/
}

int main(int argc, char* argv[])
{
	u32 LastFPS=0;
//	device = InitPLEVideo(vector2du32(800,600),false,false,false,DRV_USEDEFAULT,"",true,LOG_ALL);
//	device = InitPLEVideo(vector2du32(600,600),false,true,false,DRV_USEDEFAULT,"",true,LOG_ALL);
	InitPLEVideo(vector2du32(800,800),false,true,false,false,DRV_USEDEFAULT,"",true,LOG_ALL);
	GetDevice()->SetCaption("PLETest");

	Camera3D = SceneManager->GetSceneGenerator()->GetNew3DCameraSceneNode((vPos)90.0,(vPos)1.0,vector3dvpos(0.0,0.0,0.0),(vPos)0.1,(vPos)100.0,
		vector3dvpos(0.0,(vPos)1.0,0.0),vector3dvpos(0.0,0.0,(vPos)3.0));
	Camera3D->SetParent(SceneManager);
	Camera2D = SceneManager->GetSceneGenerator()->GetNew2DCameraSceneNode();
	Camera2D->SetParent(SceneManager);

	SceneManager->GetSceneGenerator()->SetDefaultScene3DParent(Camera3D);
	SceneManager->SetActive3DCamera(Camera3D);
	SceneManager->GetSceneGenerator()->SetDefaultScene2DParent(Camera2D);
	SceneManager->SetActive2DCamera(Camera2D);
//	Camera2D->SetScale(2.0,2.0,2.0);

	SceneManager->GetActive3DCamera()->ActivateDebugMovementType1();
	SceneManager->GetActive3DCamera()->ActivateDebugMovementType2();
	SceneManager->GetActive3DCamera()->ActivateDebugMovementType3();

//	FileSystem->SetWorkingDirectoryToStartDirectory();
	FileSystem->ReadActualDirectory();
	FileSystem->ReadAttachedFileSystem(Data,"Graphics.pfs");

	li = SceneManager->GetSceneGenerator()->GetNewLightSceneNode();
	li->SetPosition(-1.0,0.0,-20.0);
	li->SetSpotDirection(vector3df32(0.0,0.0,1.0));
	li->SetSpotCutOff(40.0);
	Cube = SceneManager->GetSceneGenerator()->GetNewCubeSceneNode();
	IImage *ImgTmp = FileSystem->GetImage("prueba.gff");
	ITexture *TxtTmp = VideoDriver->CreateTextureFromImage(ImgTmp);
//	Cube->GetMaterial()->SetTexture(TxtTmp);
	Cube->SetTextures(TxtTmp);
	Cube->SetPosition(vector3dvpos(0.0,0.0,(vPos)-0.01));
//	SceneManager->AddMoveAnimator(Cube,vector3dvpos(0.0,0.0,0.0),vector3dvpos(0.0,0.0,0.0),4000);
//	SceneManager->AddMoveAnimator(li,vector3dvpos((vPos)-0.05,(vPos)0.0,(vPos)-10.0),vector3dvpos((vPos)0.05,(vPos)0.0,(vPos)-10.0),4000);
//	Cube->SetVisible(false);
//	IDrawableSceneNode *Sphere1 = SceneManager->GetSceneGenerator()->GetNewSphereSceneNode();

//	IImage *ImgFont = FileSystem->GetImage("font.gff");
//	IFont *Font = VideoDriver->CreateFontFromImage(ImgFont);
//	GetGUI()->CreateDefaultConsole();
//	ITextSceneNode *txt1 = SceneManager->GetSceneGenerator()->GetNewTextNode(Font,"@",
//		ALIGNEMENT_X_RIGHT,ALIGNEMENT_Y_UNDER);
//	ISceneNode *Sprite = SceneManager->GetSceneGenerator()->GetNew2DSpriteSceneNode(TxtTmp);
//	Sprite->SetPositionZ(0.7);
//	ITextSceneNode *txt1 = SceneManager->GetSceneGenerator()->GetNewTextNode(Font,"@",ALIGNEMENT_X_CENTER,ALIGNEMENT_Y_MIDLE);
//	txt1->SetForeColor(CColor(1.0,0.0,0.0,0.1));
//	txt1->SetMaterialAmbients(CColor((vPos)1.0,0.0,0.0,(vPos)0.1));
//	Cube->SetMaterialAmbients(CColor((vPos)1.0,0.0,0.0,(vPos)0.1));
//	SceneManager->GetDebugVars()->SetFont(Font);
//	SceneManager->GetDebugVars()->SetScaleX(2.0);
//	SceneManager->GetDebugVars()->SetScaleY(2.0);

	while (Device->Update())
	{
		u32 ActualFPS = VideoDriver->GetFPS();
		if (ActualFPS != LastFPS)
		{
			stringc caption = "PLETest FPS:";
			caption += Device->ConvertFromNumber(ActualFPS);
			Device->SetCaption(caption);
			LastFPS = ActualFPS;
		}

		Update();

//		SceneManager->GetDebugVars()->Addu32("Prueba",234523);
//		Cube = SceneManager->GetDebugVars()->GetChilds().front();

		VideoDriver->BeginRender();
		SceneManager->RenderAll(TimeManager->GetActualTime());
		VideoDriver->EndRender();
		TimeManager->UpdateFrameTime();
	}

//	delete Font;
	delete TxtTmp;
	delete ImgTmp;
	DestroyDevice();
//	_CrtDumpMemoryLeaks();

	return 0;
}
